package ch.usi.inf.pf2.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import ch.usi.inf.pf2.gui.chart.JSpreadSheetChart;
import ch.usi.inf.pf2.gui.listener.CopyActionListener;
import ch.usi.inf.pf2.gui.listener.CutActionListener;
import ch.usi.inf.pf2.gui.listener.NewActionListener;
import ch.usi.inf.pf2.gui.listener.OpenActionListener;
import ch.usi.inf.pf2.gui.listener.PasteActionListener;
import ch.usi.inf.pf2.gui.listener.RedoActionListener;
import ch.usi.inf.pf2.gui.listener.SaveActionListener;
import ch.usi.inf.pf2.gui.listener.SaveAsActionListener;
import ch.usi.inf.pf2.gui.listener.UndoActionListener;

/**
 * The tool-bar menu
 * 
 * @author Lorenzo Baracchi
 * 
 */
public class SpreadSheetToolbar extends JPanel
{

    private static final long serialVersionUID = 1L;
    private JSpreadSheetGUI spreadTable;

    // private JPanel toolBar;

    public SpreadSheetToolbar(JSpreadSheetGUI spreadTable)
    {
	   this.spreadTable = spreadTable;
	   this.setLayout(new FlowLayout(FlowLayout.LEFT));
    }

    /**
     * Create the cool tool-bar
     */
    public void makeToolBar()
    {
	   // New button
	   JButton newSheet = new JButton(new ImageIcon("Icons/new.gif"));
	   newSheet.addActionListener(new NewActionListener(spreadTable));
	   newSheet.setToolTipText("Open a new SpreadSheet");
	   this.add(newSheet);

	   // Save button
	   JButton save = new JButton(new ImageIcon("Icons/save.gif"));
	   save.setEnabled(false);
	   save.addActionListener(new SaveActionListener(spreadTable));
	   save.setToolTipText("Save the file");
	   this.add(save);

	   // Save As button
	   JButton saveAs = new JButton(new ImageIcon("Icons/saveAs.gif"));
	   saveAs.addActionListener(new SaveAsActionListener(spreadTable, save));
	   saveAs.setToolTipText("Save the file to the specified destination");
	   this.add(saveAs);

	   // Open button
	   JButton open = new JButton(new ImageIcon("Icons/open.gif"));
	   open.addActionListener(new OpenActionListener(spreadTable));
	   open.setToolTipText("Open a file");
	   this.add(open);

	   // separator
	   JSeparator sep = new JSeparator(SwingConstants.VERTICAL);
	   this.add(sep);

	   // Undo Button
	   JButton undo = new JButton(new ImageIcon("Icons/undo.gif"));
	   undo.addActionListener(new UndoActionListener(spreadTable));
	   undo.setToolTipText("Undo last action");
	   this.add(undo);

	   // Redo Button
	   JButton redo = new JButton(new ImageIcon("Icons/redo.gif"));
	   redo.addActionListener(new RedoActionListener(spreadTable));
	   redo.setToolTipText("Redo last undone action");
	   this.add(redo);

	   // separator
	   JSeparator sep2 = new JSeparator(SwingConstants.VERTICAL);
	   this.add(sep2);

	   // Cut button
	   JButton cut = new JButton(new ImageIcon("Icons/cut.gif"));
	   cut.addActionListener(new CutActionListener(spreadTable));
	   cut.setToolTipText("Cut the cell's contents");
	   this.add(cut);

	   // Copy button
	   JButton copy = new JButton(new ImageIcon("Icons/copy.gif"));
	   copy.addActionListener(new CopyActionListener(spreadTable));
	   copy.setToolTipText("Copy the cell's contents");
	   this.add(copy);

	   // Paste button
	   JButton paste = new JButton(new ImageIcon("Icons/paste.gif"));
	   paste.addActionListener(new PasteActionListener(spreadTable));
	   paste.setToolTipText("Paste in the cell");
	   this.add(paste);
	   
	   // separator
	   JSeparator sep3 = new JSeparator(SwingConstants.VERTICAL);
	   this.add(sep3);
	   
	   // formula button
	   JButton formula = new JButton(new ImageIcon("Icons/formula.gif"));
	   formula.addActionListener(new ActionListener()
	   {
	       @Override
	       public void actionPerformed(ActionEvent arg0)
	       {
	     	 new SpreadSheetFormulaWindow(spreadTable);
	       }
	   });
	   formula.setToolTipText("Insert a formula in the selected cell");
	   this.add(formula);
	   
	   // chart button
	   JButton chart = new JButton(new ImageIcon("Icons/chart.gif"));
	   chart.addActionListener(new ActionListener()
	   {
	       @Override
	       public void actionPerformed(ActionEvent arg0)
	       {
	   	   	new JSpreadSheetChart(spreadTable);
	       }
	   });
	   this.add(chart);
    }

    public void setSpreadTable(JSpreadSheetGUI spreadTable)
    {
	   this.spreadTable = spreadTable;
    }

    public JSpreadSheetGUI getSpreadTable()
    {
	   return spreadTable;
    }

}
